Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
A designer, on the other hand, provides a visual design window within the Visual Basic development environment. The Visual Basic forms designer is included by default in all versions of the development environment. The Professional and Enterprise editions of Visual Basic provide additional designers. ### Properties Make The Object Every Visual Basic object has a set of properties that controls how it looks and behaves. This is true even of insertable objects, although their property sets are relatively small, because so much of their behavior is determined by the parent application and not by Visual Basic. Rather than just talking about properties, let’s create a simple Visual Basic project that demonstrates how they work. This program will display text in a Text Box, permitting the user to change the size of the Text Box and the color of the text. ### Starting To Design The Interface This project is designed specifically to illustrate the relationships between objects, properties, and code. It consists of a single dialog box with eight controls. I’ll be honest and admit that the program does not do anything useful, but I believe it will be a valuable learning tool. To start, fire up Visual Basic; if it is already running, select New from the File menu. Choose Standard EXE from the New Project dialog box. The first control we’ll place on the form is a Text Box. Using the techniques you learned earlier in this chapter, place a Text Box in the upper left corner of the form. The exact size doesn’t matter—perhaps two grid units high by eight units wide—you can always adjust it later. There are other controls to be placed, but first let’s set the properties for the Text Box that we just added. Changing The Text Property Make sure that the Text Box is selected, then take a look at the Properties window. If this window is not displayed, press F4. The Properties window displays the properties of the selected object, with the object’s name (that is, its Name property) displayed in the title bar. Just below the title bar is a box that displays the object’s name and its type. This box is like a Visual Basic Combo Box control—you can click on the arrow at the right end of the box to see a list of all the controls on the current form. Select a control by choosing it from the list, just as if you had clicked on it on the form. The list in the Properties window lists all of the control’s properties, either in alphabetical order (on the Alphabetic tab) or organized by category (on the Categorized tab). The property names are in the left column, and the property values in the right column. You use the scroll bar to display different properties, then select a specific property by clicking on it. The area at the bottom of the Properties window displays a brief description of the highlighted property. Scroll until the Text property is visible—on the Categorized tab, it is listed in the Misc section. The default value for this is Text1, as shown in Figure 3.4. The Text property of a Text Box specifies the text that is displayed in the control. We need to change the Text property of this control. Follow these steps: 1. Scroll the Properties list to bring the Text property into view. 2. Double-click on the Text property name to highlight the property value in the right column. 3. Type in the new text. I used “Visual Basic”, but you are welcome to use any short phrase you like. The text you type will replace the highlighted text. 4. Press Enter, select another property, or click on the form to end editing. If you want to cancel your changes and reset the property to its original value, press Esc. Notice that the new property is reflected not only in the Properties window, but also in the Text Box control on the form. Suppose you made a typing error while entering the Text property. Do you have to go back and retype the whole thing? Not at all. If you click on the property value in the right column of the Properties window, Visual Basic will display an editing cursor at the end of the text, allowing you to edit the text using the regular editing keys. Here are brief descriptions of each of the keys: • The left and right arrows move the cursor one character at a time. • Ctrl+Left arrow and Ctrl+Right arrow move the cursor one word at a time. Figure 3.4 Changing the Text Box control’s Text property. • The Home and End keys move the cursor to the start or end of the text. • The Del key erases selected text or the character to the right of the cursor. • The Backspace key erases the character to the left of the cursor. • New text is inserted at the cursor. • To select text, drag over it with the cursor. Changing The Font Property Next, we’ll change the Text Box’s Font property. As you can guess, this property determines the font used for the text in the control. Scroll the Properties list to bring the Font property into view and click once on the property name. A small button with three dots displays next to the current font value. Click on the button to display the Font dialog box, which is shown in Figure 3.5. You can also double-click on the Font property name to go directly to the dialog box. Select from the Font, Font Style, and Size lists to specify the font you want, then click on OK. I suggest using a font 12 points in size. The new font is reflected immediately in the Text Box on the form. Depending on the size of the font you selected, you may need to increase the size of the Text Box to display all of the text. ### Adding The Other Controls Now that the first control is complete, we’ll add all of the remaining controls and then change their properties as needed. We need three Command Buttons, three Option Buttons, and one Frame. What is the Frame control for? Think back to our discussion of Option Button controls. I mentioned that only one Option Button in a group can be set to on at any time. The obvious question is, “How does one define a group of option buttons?” By placing them in a Frame, that’s how. If you want more than one group of Option Buttons on a form, you need one Frame for each group. Figure 3.5 Changing the control’s Font property. Placing Option Buttons in a Frame requires a specific technique. If you add some Option Buttons to a form, add a Frame, and finally drag the Option Buttons onto the Frame, they will certainly appear to be in the Frame, but Visual Basic will not treat them as a group. To have a set of Option Buttons treated as a group, you have to put the Frame on the form first, then draw the Option Buttons within the Frame. Here are the steps required: 1. In the lower left corner of the form, place a Frame large enough to hold three Option Buttons. 2. Click on the Option Button icon in the toolbox. 3. Point at the interior of the Frame and drag to place the Option Button. 4. Repeat Steps 2 and 3 to place the additional Option Buttons. Next, add the three Command Buttons, placing them in the lower right area of the form. At this point, the form will look more or less like Figure 3.6. Before changing the properties of these newly added controls, we should save the project. Select Save Project from the File menu. Because the project and form files have not been given names yet, Visual Basic will prompt you for names. I used “Properties Demo” for the project and “Properties” for the form, but you can assign other names if you like.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.